package com.bltec.emergency.controller.statisticsController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.ResStatistics;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.service.statisticsService.ResStatisticsService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/resStatistics")
@RequiresPermissions("project:statistics")
public class ResStatisticsController {
    @Resource
    private ResStatisticsService resStatisticsService;
    @Resource
    private UserService userService;

    @RequestMapping("getAllResStatistics")
    public ResponseJson getAllResStatistics() {
        try {
            List<ResStatistics> resStatistics = resStatisticsService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(resStatistics));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /*    private String area;
    private String type;
    private Long departmentId;
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date time;
    private Long totalRealized;
    private Long totalType;
    private Long totalNumber;
    private BigDecimal accountFor;*/
    @RequiresPermissions("statistics:insert")
    @RequestMapping("addResStatistics")
    public ResponseJson addResStatistics(String area, String type, Long departmentId, Date time, Long totalRealized, Long totalType, Long totalNumber, BigDecimal accountFor) {
        try {
            ResStatistics resStatistics = new ResStatistics();
            resStatistics.setArea(area);
            resStatistics.setType(type);
            resStatistics.setDepartmentId(departmentId);
            resStatistics.setTime(time);
            resStatistics.setTotalRealized(totalRealized);
            resStatistics.setTotalType(totalType);
            resStatistics.setTotalNumber(totalNumber);
            resStatistics.setAccountFor(accountFor);
            resStatistics.setEnable(true);
            resStatistics.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resStatistics.setCreateTime(DateUtil.dateNow());
            resStatisticsService.add(resStatistics);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    @RequiresPermissions("statistics:update")
    @RequestMapping("updateResStatistics")
    public ResponseJson updateResStatistics(Long id, String area, String type, Long departmentId, Date time, Long totalRealized, Long totalType, Long totalNumber, BigDecimal accountFor) {
        try {
            ResStatistics resStatistics = resStatisticsService.getResStatisticsById(id);
            resStatistics.setArea(area);
            resStatistics.setType(type);
            resStatistics.setDepartmentId(departmentId);
            resStatistics.setTime(time);
            resStatistics.setTotalRealized(totalRealized);
            resStatistics.setTotalType(totalType);
            resStatistics.setTotalNumber(totalNumber);
            resStatistics.setAccountFor(accountFor);
            resStatistics.setEnable(true);
            resStatistics.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            resStatistics.setUpdateTime(DateUtil.dateNow());
            resStatisticsService.updateResStatisticsById(resStatistics);
            return new ResponseJson(Constant.OK, JSON.toJSON(resStatistics));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    @RequiresPermissions("statistics:delete")
    @RequestMapping("deleteResStatistics")
    public ResponseJson setDisable(Long id) {
        if (null != resStatisticsService.getResStatisticsById(id)) {
            if (resStatisticsService.deleteResStatisticsById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }
}
